//
//  PywInitApi.m
//  PywSdk
//
//  Created by Jade on 16/8/4.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywInitApi.h"
#import "AIFInitManager.h"
#import "PropertyListReformer.h"
#import "PywMBProgressHUD+Pyw.h"
#import "PywCPNotifications.h"
#import "PywSdk+login.h"
@interface PywInitApi()<ETAPIManagerCallBackDelegate>
@end
@implementation PywInitApi
- (void)requestInitWithSuccess:(Successd)success
                          fail:(Failure)fail
{
    self.successd = success;
    self.failure = fail;
    // 初始化请求
    AIFInitManager *manager = [[AIFInitManager alloc] init];
    manager.delegate = self;
    [manager loadData];
    
}

#pragma mark - ETAPIManagerCallBackDelegate
- (void)managerCallAPIDidSuccess:(ETApiBaseManager *)manager
{
    PropertyListReformer *reformer = [[PropertyListReformer alloc] init];
    NSDictionary *dict = [manager fetchDataWithReformer:reformer];
    int resultCode = [[dict objectForKey:@"ack"] intValue];
    if (200 == resultCode) {
        NSMutableDictionary *data = [dict objectForKey:@"data"];
        [PywAppInfo sharedAppInfo].gameId = [data objectForKey:@"gameid"];
        [PywAppInfo sharedAppInfo].apisecret = [data objectForKey:@"apisecret"];
        [PywAppInfo sharedAppInfo].notice_url = [data objectForKey:@"common_public_notice_url"];//公告地址
        // 有关上传登录游戏账号时长参数
        [PywPlatformMain sharedPlatformMain].collect_to_where = [data objectForKey:@"collect_to_where"];
        [PywPlatformMain sharedPlatformMain].collect_open = [[data objectForKey:@"collect_open"] boolValue];
        [PywPlatformMain sharedPlatformMain].collect_interval = [[data objectForKey:@"collect_interval"] integerValue];
        
        // 苹果内购支付
        [PywAppInfo sharedAppInfo].is_applepay = [[data objectForKey:@"is_applepay"] boolValue];
        // 设置“绑定”对话框弹出时间
        [PywAppInfo sharedAppInfo].dialog_time = [[data objectForKey:@"dialog_time"] integerValue];
        // 删除账号 按钮
        [PywPlatformMain sharedPlatformMain].is_deleteAccountBtn = [[data objectForKey:@"del_button"] boolValue];
        // 快速注册
        [PywPlatformMain sharedPlatformMain].is_fastRegister = [[data objectForKey:@"fast_reg"] boolValue];
        // 是否开启“自动注册并登录”
        [PywPlatformMain sharedPlatformMain].is_autoRegisterLogin = [[data objectForKey:@"auto_login"] boolValue];
        // 手机注册
        [PywPlatformMain sharedPlatformMain].is_reg_open = [[data objectForKey:@"reg_open"] boolValue];
        // 是否显示悬浮窗
        [PywAppInfo sharedAppInfo].is_float = [[data objectForKey:@"float_show"] boolValue];
        // 第三方支付开关
        NSDictionary *payment = [data objectForKey:@"payment"];
        [PywAppInfo sharedAppInfo].is_unipay = [[payment objectForKey:@"unipay"] boolValue];
        [PywAppInfo sharedAppInfo].is_alipay = [[payment objectForKey:@"alipay"] boolValue];
        [PywAppInfo sharedAppInfo].is_wxpay = [[payment objectForKey:@"wechatpay"] boolValue];
        [PywAppInfo sharedAppInfo].pay_type_order = [data objectForKey:@"pay_type_order"];
        //历史登录用户
        NSArray *game_account = [data objectForKey:@"game_account"];
        NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:game_account.count];
        for (NSDictionary *dict in game_account) {
            [array addObject:[dict objectForKey:@"username"]];
        }
        [PywAppInfo sharedAppInfo].game_cache_account = array;
        
        // 初始化注销状态
        [PywPlatformMain sharedPlatformMain].logouted = NO;
        [PywPlatformMain sharedPlatformMain].initialized = YES;
        // 发送初始化完成的通知
        [self sendInitSuccessNotification];
        
    }else{
        [PywMBProgressHUD showError:[dict objectForKey:@"msg"]];
        // 发送初始化失败的通知
        [self sendInitFailNotification];
    }
    
}

- (void)managerCallApiDidFailed:(ETApiBaseManager *)manager{
    [PywPlatformMain sharedPlatformMain].logouted = NO;
    [PywPlatformMain sharedPlatformMain].initialized = NO;
    // 发送初始化失败的通知
    [self sendInitFailNotification];
}

/**
 *  初始化成功回调
 */
- (void)sendInitSuccessNotification
{
    if (self.successd) {
        self.successd(nil,nil);
    }
    NSMutableDictionary *mDict = [[NSMutableDictionary alloc] init];
    [mDict setObject:[NSNumber numberWithInt:PYW_INIT_STATE_SUCCESS] forKey:@"result"];
    [mDict setObject:@"初始化成功" forKey:@"info"];
    [[NSNotificationCenter defaultCenter] postNotificationName:kPywCPInitNotification object:nil userInfo:mDict];
}
/**
 *  初始化失败回调
 */
- (void)sendInitFailNotification
{
    if (self.failure) {
        self.failure(nil);
    }
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setObject:[NSNumber numberWithInt:PYW_INIT_STATE_FAILURE] forKey:@"result"];
    [dict setObject:@"初始化失败" forKey:@"info"];
    [[NSNotificationCenter defaultCenter] postNotificationName:kPywCPInitNotification object:nil userInfo:dict];
}

@end
